﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2006
' by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
 */

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Web;
using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Search;

namespace Triax.Modules.Department
{
    /// -----------------------------------------------------------------------------
    ///<summary>
    /// The Controller class for the ContractTemplate
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// </history>
    /// -----------------------------------------------------------------------------
    public class ContractTemplateController : ISearchable, IPortable
    {

        #region Constructors

        public ContractTemplateController()
        {
        }

        #endregion

        #region Public Methods

        public List<ContractTemplate> HDChuaTT(List<ContractTemplate> colDepartments)
        {
            List<ContractTemplate> loc = new List<ContractTemplate>();
            foreach (ContractTemplate obj in colDepartments)
            {
                if (obj.Tongso < obj.IsThanhLyDoiTac)
                {
                    loc.Add(obj);
                }
            }
            return loc;
        }
        public List<ContractTemplate> HDDaTT(List<ContractTemplate> colDepartments)
        {
            List<ContractTemplate> loc = new List<ContractTemplate>();
            foreach (ContractTemplate obj in colDepartments)
            {
                if (obj.Tongso == obj.IsThanhLyDoiTac)
                {
                    loc.Add(obj);
                }
            }
            return loc;
        }
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// adds an object to the database
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="objContractTemplate">The ContractTemplate object</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public void AddContractTemplate(ContractTemplate objContractTemplate)
        {
            // if (objContractTemplate.ContractTemplateName.Trim() != "")
            {
                DataProvider.Instance().AddContractTemplate(objContractTemplate.ModuleId,
                    objContractTemplate.TieuDe,
objContractTemplate.NoiDung,
objContractTemplate.Nguoitao,
objContractTemplate.KtCheck1,
objContractTemplate.Khcheck1,
objContractTemplate.Type,
objContractTemplate.BienTap1,
objContractTemplate.ThamDinh1,
objContractTemplate.IsXuatBan,
objContractTemplate.IsGiayPhep,
objContractTemplate.IsPrint,
objContractTemplate.CheckConten,
objContractTemplate.PrintTo,
objContractTemplate.checkSanpham,
objContractTemplate.IsLuuChieu,
objContractTemplate.IsPhatHanh,
objContractTemplate.IsThanhLyDoiTac,
objContractTemplate.IsThanhLyKhachHang,
objContractTemplate.Ngaythuchien,
objContractTemplate.hancuoi,
objContractTemplate.ngayKetThuc,
objContractTemplate.InsertDate,
objContractTemplate.UpdateDate,
objContractTemplate.CreateByUser,
objContractTemplate.UpdateByUser,
objContractTemplate.FileHopDong,
objContractTemplate.FileThanhLy,
objContractTemplate.FileKhac,
objContractTemplate.IsDelete,
objContractTemplate.tempInt1,
objContractTemplate.tempInt2,
objContractTemplate.tempInt3,
objContractTemplate.tempInt4,
objContractTemplate.tempInt5,
objContractTemplate.tempInt6,
objContractTemplate.tempStr1,
objContractTemplate.tempStr2,
objContractTemplate.tempStr3,
objContractTemplate.tempStr4,
objContractTemplate.tempStr5,
objContractTemplate.tempStr6,
objContractTemplate.tempDate1,
objContractTemplate.tempDate2,
objContractTemplate.tempDate3,
objContractTemplate.tempDate4,
objContractTemplate.BT_thamdinhbanthao,
objContractTemplate.BT_thongnhatnoidungkhachhang,
objContractTemplate.BT_trinhquxb,
objContractTemplate.BT_kiemtratruockhiin,
objContractTemplate.CheBan_cheban,
objContractTemplate.CheBan_docbong,
objContractTemplate.CheBan_inbong,
objContractTemplate.CongNghe_giamsatnhain,
objContractTemplate.CongNghe_kiemtrasanpham,
objContractTemplate.KinhDoanh_nhansanpham,
objContractTemplate.GiamDoc_LuuChieu,
objContractTemplate.GiamDoc_quyetdinhxuatban,
objContractTemplate.GiamDocChuyenPhatHanh,
objContractTemplate.GiamdocKhac,
objContractTemplate.CheBan_khac,
objContractTemplate.BT_khac,
objContractTemplate.Congnghe_khac,
objContractTemplate.tempStr7,
objContractTemplate.tempStr8,
objContractTemplate.tempStr9,
objContractTemplate.tempStr10,
objContractTemplate.tempStr11,
objContractTemplate.tempStr12,
objContractTemplate.tempStr13,
objContractTemplate.tempStr14,
objContractTemplate.tempStr15,
objContractTemplate.tempStr16,
objContractTemplate.tempStr17,
objContractTemplate.tempStr18,
objContractTemplate.tempStr19,
objContractTemplate.tempStr20,
objContractTemplate.tempStr21,
objContractTemplate.tempStr22,
objContractTemplate.tempStr23,
objContractTemplate.tempStr24,
objContractTemplate.tempStr25,
objContractTemplate.tempStr26,
objContractTemplate.tempStr27,
objContractTemplate.tempStr28,
objContractTemplate.tempStr29,
objContractTemplate.tempStr30



                    );
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// deletes an object from the database
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="ModuleId">The Id of the module</param>
        /// <param name="ItemId">The Id of the item</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public void DeleteContractTemplate(int ModuleId, int ContractTemplateId)
        {
            DataProvider.Instance().DeleteContractTemplate(ModuleId, ContractTemplateId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// gets an object from the database
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="moduleId">The Id of the module</param>
        /// <param name="ItemId">The Id of the item</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public ContractTemplate GetContractTemplate(int ModuleId, int ContractTemplateId)
        {
            return CBO.FillObject<ContractTemplate>(DataProvider.Instance().GetContractTemplate(ModuleId, ContractTemplateId));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// gets an object from the database
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="moduleId">The Id of the module</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public List<ContractTemplate> GetContractTemplates(int ModuleId)
        {
            return CBO.FillCollection<ContractTemplate>(DataProvider.Instance().GetContractTemplates(ModuleId));
        }

        public List<ContractTemplate> GetContractTemplatesByCond(int ModuleId, string cond, string sql)
        {
            return CBO.FillCollection<ContractTemplate>(DataProvider.Instance().GetContractTemplatesByCond(ModuleId, cond, sql));
        }

        public List<ContractTemplate> GetContractTemplatesByCondIn(int ModuleId, string cond, string sql)
        {
            return CBO.FillCollection<ContractTemplate>(DataProvider.Instance().GetContractTemplatesByCondIn(ModuleId, cond, sql));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// saves an object to the database
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="objContractTemplate">The ContractTemplate object</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public void UpdateContractTemplate(ContractTemplate objContractTemplate)
        {
            // if (objContractTemplate.ContractTemplateName.Trim() != "")
            {
                DataProvider.Instance().UpdateContractTemplate(objContractTemplate.ModuleId,
                    objContractTemplate.ContractTemplateId,
objContractTemplate.TieuDe,
objContractTemplate.NoiDung,
objContractTemplate.Nguoitao,
objContractTemplate.KtCheck1,
objContractTemplate.Khcheck1,
objContractTemplate.Type,
objContractTemplate.BienTap1,
objContractTemplate.ThamDinh1,
objContractTemplate.IsXuatBan,
objContractTemplate.IsGiayPhep,
objContractTemplate.IsPrint,
objContractTemplate.CheckConten,
objContractTemplate.PrintTo,
objContractTemplate.checkSanpham,
objContractTemplate.IsLuuChieu,
objContractTemplate.IsPhatHanh,
objContractTemplate.IsThanhLyDoiTac,
objContractTemplate.IsThanhLyKhachHang,
objContractTemplate.Ngaythuchien,
objContractTemplate.hancuoi,
objContractTemplate.ngayKetThuc,
objContractTemplate.InsertDate,
objContractTemplate.UpdateDate,
objContractTemplate.CreateByUser,
objContractTemplate.UpdateByUser,
objContractTemplate.FileHopDong,
objContractTemplate.FileThanhLy,
objContractTemplate.FileKhac,
objContractTemplate.IsDelete,
objContractTemplate.tempInt1,
objContractTemplate.tempInt2,
objContractTemplate.tempInt3,
objContractTemplate.tempInt4,
objContractTemplate.tempInt5,
objContractTemplate.tempInt6,
objContractTemplate.tempStr1,
objContractTemplate.tempStr2,
objContractTemplate.tempStr3,
objContractTemplate.tempStr4,
objContractTemplate.tempStr5,
objContractTemplate.tempStr6,
objContractTemplate.tempDate1,
objContractTemplate.tempDate2,
objContractTemplate.tempDate3,
objContractTemplate.tempDate4,
objContractTemplate.BT_thamdinhbanthao,
objContractTemplate.BT_thongnhatnoidungkhachhang,
objContractTemplate.BT_trinhquxb,
objContractTemplate.BT_kiemtratruockhiin,
objContractTemplate.CheBan_cheban,
objContractTemplate.CheBan_docbong,
objContractTemplate.CheBan_inbong,
objContractTemplate.CongNghe_giamsatnhain,
objContractTemplate.CongNghe_kiemtrasanpham,
objContractTemplate.KinhDoanh_nhansanpham,
objContractTemplate.GiamDoc_LuuChieu,
objContractTemplate.GiamDoc_quyetdinhxuatban,
objContractTemplate.GiamDocChuyenPhatHanh,
objContractTemplate.GiamdocKhac,
objContractTemplate.CheBan_khac,
objContractTemplate.BT_khac,
objContractTemplate.Congnghe_khac,
objContractTemplate.tempStr7,
objContractTemplate.tempStr8,
objContractTemplate.tempStr9,
objContractTemplate.tempStr10,
objContractTemplate.tempStr11,
objContractTemplate.tempStr12,
objContractTemplate.tempStr13,
objContractTemplate.tempStr14,
objContractTemplate.tempStr15,
objContractTemplate.tempStr16,
objContractTemplate.tempStr17,
objContractTemplate.tempStr18,
objContractTemplate.tempStr19,
objContractTemplate.tempStr20,
objContractTemplate.tempStr21,
objContractTemplate.tempStr22,
objContractTemplate.tempStr23,
objContractTemplate.tempStr24,
objContractTemplate.tempStr25,
objContractTemplate.tempStr26,
objContractTemplate.tempStr27,
objContractTemplate.tempStr28,
objContractTemplate.tempStr29,
objContractTemplate.tempStr30




                    );
            }
        }
        // End kết hợp xử lý
        #endregion

        #region Optional Interfaces

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// GetSearchItems implements the ISearchable Interface
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="ModInfo">The ModuleInfo for the module to be Indexed</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public SearchItemInfoCollection GetSearchItems(ModuleInfo ModInfo)
        {
            SearchItemInfoCollection SearchItemCollection = new SearchItemInfoCollection();
            List<ContractTemplate> colContractTemplates = GetContractTemplates(ModInfo.ModuleID);

            foreach (ContractTemplate objContractTemplate in colContractTemplates)
            {
                if (objContractTemplate != null)
                {
                    SearchItemInfo SearchItem = new SearchItemInfo();
                    SearchItemCollection.Add(SearchItem);
                }
            }

            return SearchItemCollection;
        }


        /// -----------------------------------------------------------------------------
        /// <summary>
        /// ExportModule implements the IPortable ExportModule Interface
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="ModuleID">The Id of the module to be exported</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public string ExportModule(int ModuleID)
        {
            string strXML = "";
            List<ContractTemplate> colContractTemplates = GetContractTemplates(ModuleID);

            if (colContractTemplates.Count != 0)
            {
                strXML += "<ContractTemplates>";
                foreach (ContractTemplate objContractTemplate in colContractTemplates)
                {
                    strXML += "<ContractTemplate>";
                    strXML += "<content>" + XmlUtils.XMLEncode(objContractTemplate.tempStr1) + "</content>";
                    strXML += "</ContractTemplate>";
                }
                strXML += "</ContractTemplates>";
            }

            return strXML;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// ImportModule implements the IPortable ImportModule Interface
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="ModuleID">The Id of the module to be imported</param>
        /// <param name="Content">The content to be imported</param>
        /// <param name="Version">The version of the module to be imported</param>
        /// <param name="UserId">The Id of the user performing the import</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public void ImportModule(int ModuleID, string Content, string Version, int UserId)
        {
            XmlNode xmlContractTemplates = Globals.GetContent(Content, "ContractTemplates");

            foreach (XmlNode xmlContractTemplate in xmlContractTemplates.SelectNodes("ContractTemplate"))
            {
                ContractTemplate objContractTemplate = new ContractTemplate();

                objContractTemplate.ModuleId = ModuleID;
                //    objContractTemplate.ContractTemplateName = xmlContractTemplate.SelectSingleNode("content").InnerText;
                //      objContractTemplate.CreatedByUser = UserId;
                AddContractTemplate(objContractTemplate);
            }

        }

        public void addUserRoleList(int portalid, int userid, string roleadd, string rolelist)
        {
            string strs = "domain-hosting;Lap-trinh;Nhap-lieu;Pre-Sale;Sale;SEO;TeleSale;Thiet-ke;tracuuhost;vanphong";

            string[] roleaadds = roleadd.Split(';');
            string[] rolelists = rolelist.Split(';');

            DotNetNuke.Security.Roles.RoleController rc = new DotNetNuke.Security.Roles.RoleController();


            if (roleaadds.Length > 0)
            {
                // xóa các role cũ
                for (int i = 0; i < rolelists.Length; i++)
                {
                    DotNetNuke.Security.Roles.RoleInfo ri = rc.GetRoleByName(portalid, rolelists[i]);
                    if (null != ri)
                    {
                        //rc.AddUserRole(PortalId, userInfo.UserID, ri.RoleID, Null.NullDate);
                        bool isdel = rc.DeleteUserRole(portalid, userid, ri.RoleID);
                    }
                }

                // thêm role
                for (int i = 0; i < roleaadds.Length; i++)
                {
                    DotNetNuke.Security.Roles.RoleInfo ri = rc.GetRoleByName(portalid, roleaadds[i]);
                    if (null != ri)
                    {
                        rc.AddUserRole(portalid, userid, ri.RoleID, Null.NullDate);
                        //  rc.DeleteUserRole(PortalId, userid, ri.RoleID);
                    }
                }


            }
        }

        #endregion

    }
}

